<?php

namespace app\api\controller\web\frontend;

use app\common\controller\Api;
use think\facade\Db;


// 季华实验室  --科研成果
class Scientific extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
    public function list()
    {
        // 分类
        $keywords = input('keywords') ?? '';
        $team_id = input('team_id') ?? '';
        $where = [];
        if ($keywords) {
            $where[] = ['title', 'like', '%'.$keywords.'%'];
        }
        if ($team_id) {
            $team_name = Db::name('web_scientific_team')->where('id',$team_id)->value('team_name');
            $where[] = ['team_name', '=', $team_name];
        }

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;
        
        $list = Db::name('web_scientific')
                        ->where($where)
                        ->order('weigh desc')
                        ->limit($start, $page_size)
                        ->select()
                        ->toArray();
        
        foreach ($list as $key => $value) {
            $list[$key]['createtime'] = date('Y-m-d', $value['createtime']);
            $list[$key]['content'] = strip_tags(htmlspecialchars_decode($value['content']));
        }
        
        $count = Db::name('web_scientific')->where($where)->count();

        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);
    }


    /**
     * 详情
     */
    public function info()
    {
        $id = input('scientific_id');
        if (!$id) {
            $this->error('请求参数出错');
        }

        $data = Db::name('web_scientific')->find($id);
        if (empty($data)) {
            $this->error('不存在的数据');
        }

        // 数据处理
        $data['createtime'] = date('Y-m-d', $data['createtime']);
        $data['content'] = htmlspecialchars_decode($data['content']);

        // 上一篇 下一篇
        // 查所有 取索引（后期数据量大 卡慢）
        $data['prev'] = Db::name('web_scientific')->where('id', '<', $id)->max('id');
        $data['next'] = Db::name('web_scientific')->where('id', '>', $id)->min('id');

        $this->success('success',$data);

    }

    // 科研团队列表
    public function team()
    {
        // 分类
        $keywords = input('keywords') ?? '';
        $where = [];
        if ($keywords) {
            $where[] = ['team_name', 'like', '%'.$keywords.'%'];
        }

        $where[] = ['is_showswitch','=',1];
        $where[] = ['deletetime','=',null];

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;

        $list = Db::name('web_scientific_team')
                        ->field('id,team_name,tpic_image')
                        ->where($where)
                        ->order('weigh desc')
                        ->limit($start, $page_size)
                        ->select()
                        ->toArray();
        
        foreach ($list as $key => $value) {
            $list[$key]['count'] = Db::name('web_scientific')->where('team_name',$value['team_name'])->count();
        }
        
        $count = Db::name('web_scientific_team')->where($where)->count();

        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);

    }

    //团队详情
    public function team_info()
    {
        $id = input('team_id');
        if (!$id) {
            $this->error('请求参数出错');
        }

        $data = Db::name('web_scientific_team')->find($id);
        // 加一个type标记为consult_type的id
        $data['type_id'] = Db::name('web_consult_type')->where('type',3)->value('id');
        if (empty($data)) {
            $this->error('不存在的数据');
        }
        $this->success('success',$data);
    }

    // 推荐团队 
    // 获取所属领域和所在院校相近的 没有则最新的三条
    public function recommend()
    {
        $id = input('team_id');
        if (!$id) {
            $this->error('请求参数出错');
        }
        // 获取传入团队的院校和领域
        $list = Db::name('web_scientific_team')->find($id);
        $colleges = $list['colleges'];
        $realm = $list['colleges'];
        $where = [];

        $where[] = ['is_showswitch','=',1];
        $where[] = ['deletetime','=',null];

        $data = Db::name('web_scientific_team')
                ->field('id,tpic_image,team_name,colleges,realm,count')
                ->where($where)
                ->where('colleges','like','%'.$colleges.'%')
                ->where('realm','like','%'.$realm.'%')
                ->limit(3)
                ->select()->toArray();
        $arr = Db::name('web_scientific_team')
                ->field('id,tpic_image,team_name,colleges,realm,count')
                ->where($where)
                ->limit(3)
                ->select()->toArray();
        $end_arr = array_merge($data,$arr);
        $this->success('success',$end_arr);
    }
    



}